package com.ebc.eservice.service.app.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ebc.eservice.mapper.CompanyArticleMapper;
import com.ebc.eservice.mapper.CompanyArticleTypeMapper;
import com.ebc.eservice.pojo.EbcCompanyArticle;
import com.ebc.eservice.pojo.EbcCompanyArticleType;
import com.ebc.eservice.pojo.EbcUser;
import com.ebc.eservice.pojo.pic.UploadFile;
import com.ebc.eservice.service.app.AppArticleService;
import com.ebc.eservice.util.JsonResult;
import com.ebc.eservice.vo.article.ArticlePicRespVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author: Joey
 * @Description:小程序端个性化内容 -文章
 * @date:2022/9/25 16:57
 */
@Service
public class ArticleAppImp implements AppArticleService {

    @Autowired
    private CompanyArticleTypeMapper TypeArticleMapper;
    @Autowired
    private CompanyArticleMapper ArticleMapper;
    @Autowired
    private MongoTemplate mongoTemplate;
    @Override
    public JsonResult getArticleList(Integer typeId) {
        QueryWrapper<EbcCompanyArticle> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("company_id",1);
        queryWrapper.eq("open",1);
        queryWrapper.eq("type_id",typeId);
        //获取对应类型的文章
        List<EbcCompanyArticle> articleList = ArticleMapper.selectList(queryWrapper);

        //查找图片
        ArrayList<Integer> list = new ArrayList<>();
        for (EbcCompanyArticle article : articleList) {
            list.add(article.getId());
        }
        ArrayList<ArticlePicRespVo> dataList = new ArrayList<>();
        ArticlePicRespVo picRespVo;
        for (Integer id : list) {
            picRespVo = new ArticlePicRespVo();
            byte[] dataArt = null;
            //条件查询器
            Query query = Query.query(Criteria.where("articleId").is(id));
            UploadFile file = mongoTemplate.findOne(query,UploadFile.class);
            if(file != null){
                dataArt = file.getContent().getData();//blob类型
                picRespVo.setArticleId(id);
                picRespVo.setDataArt(dataArt);
                dataList.add(picRespVo);
            }
        }
//        dataList.forEach(System.out::println);
        HashMap<String, Object> map = new HashMap<>();
        map.put("articleText",articleList);
        map.put("articlePic",dataList);

        return JsonResult.success(map);
    }

    @Override
    public void addViewNum(Integer articleId) {
        ArticleMapper.addViewNum(articleId);
    }

    @Override
    public JsonResult getArticleType() {
        List<EbcCompanyArticleType> types = TypeArticleMapper.selectList(null);
        return JsonResult.success(types);
    }
}
